﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_document_category_detail : BasePermissionPage
{
    public class clsMIS_CODE_DETAIL
    {
        public string Dept_ID { get; set; }
        public string Document { get; set; }
        public string Document_NAME { get; set; }
        public string DATA_SEQ { get; set; }
        public string STATUS { get; set; }
        public string USER { get; set; }

    }
    [WebMethod]
    public static Object CATEGORY(string DEPT_ID, string CATEGORY_ID)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select  dept_id, document_category, document_category_name, document_no, document_name, remark, data_seq, status ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A  ");
            m_sb.Append(" Where dept_id = :DEPT_ID ");
            m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            if (CATEGORY_ID != "")
            {
                m_sb.Append(" AND document_category = :CATEGORY_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_ID", CATEGORY_ID));
            }
            m_sb.Append(" order by dept_id , data_seq,document_category ");
            ds = m_dba.runExecuteQuery("CATEGORY", m_sb.ToString(), m_arrParam);

            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                results = m_dba.RowsToDictionary(ds.Tables["CATEGORY"], ds.Tables["CATEGORY"].Rows.Count.ToString(), "1");
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category_detail.aspx.cs", "CATEGORY", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    [WebMethod]
    public static Object getCATEGORY(string DEPT_ID, string CATEGORY_ID)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select  distinct document_category, document_category_name ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A  ");
            m_sb.Append(" Where dept_id = :DEPT_ID ");
            m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            if (CATEGORY_ID != "")
            {
                m_sb.Append(" and document_category = :CATEGORY_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_ID", CATEGORY_ID));
            }
            m_sb.Append(" order by document_category ");
            ds = m_dba.runExecuteQuery("CATEGORY", m_sb.ToString(), m_arrParam);

            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                results = ds.Tables["CATEGORY"].Rows[0]["document_category_name"].ToString();
                //m_dba.ToJson(ds,"1","1"
            }
            else
            {
                if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "100")
                {
                    results = "error_no";
                }
                else
                {
                    results = "error資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
                }
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category_detail.aspx.cs", "getCATEGORY", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    
    [WebMethod]
    public static Object Maintain_Save(string ACTION,string Dept_ID, string Category, string Category_NAME, List<clsMIS_CODE_DETAIL> DetlCtrls)
    {
        Object results = new Object();
        try
        {
            ddscDataSet m_ddscDs = new ddscDataSet();
            dbAccess m_dba = new dbAccess();
            ddscDataTable p_ddscDataTable = m_ddscDs.createDataTable("KRJ_CATEGORY_DOCUMENT");
            DataBase dataBase = new DataBase();
            // 宣告回傳DataSet
            DataSet dsResult = new DataSet();
            //先刪除

            int i = 0;
            foreach (clsMIS_CODE_DETAIL m_clsMIS_CODE_DETAIL in DetlCtrls)
            {
                if (i == 0)
                {
                    p_ddscDataTable.addNewRow();
                    p_ddscDataTable.setColumnValue("action", "NVARCHAR", "DEL");
                    p_ddscDataTable.setColumnValue("dept_id", "NVARCHAR", Dept_ID);
                    p_ddscDataTable.setWhereValue("dept_id", Dept_ID);
                    if (Category != "")
                    {
                        p_ddscDataTable.setColumnValue("document_category", "NVARCHAR", Category);
                        p_ddscDataTable.setWhereValue("document_category", Category);
                    }
                }
                if (m_clsMIS_CODE_DETAIL.Document.Trim() != "")
                {
                    p_ddscDataTable.addNewRow();
                    p_ddscDataTable.setColumnValue("action", "NVARCHAR", "ADD");
                    p_ddscDataTable.setColumnValue("dept_id", "NVARCHAR", m_clsMIS_CODE_DETAIL.Dept_ID);
                    p_ddscDataTable.setColumnValue("document_category", "NVARCHAR", Category);
                    p_ddscDataTable.setColumnValue("document_category_name", "NVARCHAR", Category_NAME);
                    p_ddscDataTable.setColumnValue("document_no", "NVARCHAR", m_clsMIS_CODE_DETAIL.Document);
                    p_ddscDataTable.setColumnValue("document_name", "NVARCHAR", m_clsMIS_CODE_DETAIL.Document_NAME);
                    p_ddscDataTable.setColumnValue("remark", "NVARCHAR", "");
                    p_ddscDataTable.setColumnValue("data_seq", "NUMBER", m_clsMIS_CODE_DETAIL.DATA_SEQ);
                    p_ddscDataTable.setColumnValue("status", "VARCHAR", m_clsMIS_CODE_DETAIL.STATUS);

                    p_ddscDataTable.setColumnValue("CRT_ID", "NVARCHAR", m_clsMIS_CODE_DETAIL.USER);
                    p_ddscDataTable.setColumnValue("CRT_DATE", "NVARCHAR", System.DateTime.Now.ToString("yyyyMMdd"));
                    p_ddscDataTable.setColumnValue("CRT_TIME", "NVARCHAR", System.DateTime.Now.ToString("HHmmss"));

                    p_ddscDataTable.setColumnValue("UPD_ID", "NVARCHAR", m_clsMIS_CODE_DETAIL.USER);
                    p_ddscDataTable.setColumnValue("UPD_DATE", "NVARCHAR", System.DateTime.Now.ToString("yyyyMMdd"));
                    p_ddscDataTable.setColumnValue("UPD_TIME", "NVARCHAR", System.DateTime.Now.ToString("HHmmss"));
                }
                else
                {
                    results = "1!";
                }
                i = i + 1;
            }

            if (p_ddscDataTable.Rows.Count > 1)
            {
                m_ddscDs.addddscDataTable(p_ddscDataTable);
            }
            // 執行編輯method
            DataBase m_DataBase = new DataBase();
            dsResult = m_DataBase.AdditionParameter(m_ddscDs);
            string strResult = dsResult.Tables[0].Rows[0]["TransResult"].ToString();
            string strMessage = dsResult.Tables[0].Rows[0]["Message"].ToString();
            string strLogMessage = dsResult.Tables[0].Rows[0]["LogMessage"].ToString();
            if (strResult == "T")
            {
                
                switch (ACTION)
                {
                    case "Insert":
                        results = "新增成功!";

                        break;
                    case "Delete":
                        results = "刪除成功!";
                        break;
                    case "Update":
                        results = "修改成功!";
                        break;
                    default:
                        results = "存檔成功!";
                        break;
                }
                
            }
            else
            {
                results = "error" + strMessage;
            }
        }
        catch (Exception ex)
        {
            //results = "error" + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = "error" + BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category_detail.aspx.cs", "Maintain_Save", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string mode="";       
            //紀錄頁次
            //判斷如果是維護頁導回來就填回查詢條件
            if (PreviousPage != null)
            {
                krj_document_category previous = PreviousPage as krj_document_category;
                DataSet ds = null;
                ds = DBINT();
                //DropDownList資料bind入
                ddldept.DataTextField = "DEPT_NAME";
                ddldept.DataValueField = "DEPT_ID";
                ddldept.DataSource = ds.Tables["DEPT"];
                ddldept.DataBind();
                
                mode = previous.Mode;
                //從前一頁帶入KEY資料
                if (mode != "Insert")
                {
                    ddldept.SelectedValue = previous.g_GridKey["DEPT_ID_H"].ToString();
                    txtCategory.Text = previous.g_GridKey["DOCUMENT_CATEGORY"].ToString();
                    txtCategory_NAME.Text = previous.g_GridKey["DOCUMENT_CATEGORY_NAME"].ToString();
                    hidddldept.Value = previous.g_GridKey["DEPT_ID_H"].ToString();
                    hidddlCategory.Value = previous.g_GridKey["DOCUMENT_CATEGORY"].ToString();
                }
            }
            switch (mode)
            {
                case "Insert":
                    btn_save.Visible = true;

                    break;
                case "View":
                    btn_save.Visible = false;
                    btnADDROW.Visible = false;
                    ddldept.Enabled = false;
                    txtCategory.Enabled = false;
                    txtCategory_NAME.Enabled = false;
                    break;
                case "Delete":
                    btn_save.Text = "刪除";
                    btn_save.Visible = true;
                    btnADDROW.Visible = false;
                    ddldept.Enabled = false;
                    txtCategory.Enabled = false;
                    txtCategory_NAME.Enabled = false;
                    
                    break;
                case "Update":
                    btn_save.Visible = true;
                    ddldept.Enabled = false;
                    txtCategory.Enabled = false;
                    break;
                default:
                    break;
            }

            ClientScript.RegisterClientScriptBlock(this.GetType(), "search", "<script language='javascript'>$(document).ready(function () {Search_Click('" + mode + "'); }) </script>");
        }
        catch (Exception ex)
        {
            //lbl_message.Text = ex.Message;
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_document_category_detail.aspx.cs", "Maintain_Save", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where A.bi4101 < '1000' ");
            m_sb.Append(" order by A.bi4101 ");
            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            if (ds.Tables[0].Rows[0]["ReturnCode"] != "000")
            {
                lbl_message.Text = "資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
            }

        }
        catch (Exception ex)
        {
            //lbl_message.Text = ex.Message;
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_document_category_detail.aspx.cs", "Maintain_Save", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    
}